package com.tool.cn;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.tool.cn.util.ExcelUtil;
import com.tool.cn.util.StringUtil;

import org.apache.commons.codec.binary.Hex;

/**
 * 程序主类
 * 
 * @author wangzh 2018年1月16日 下午5:13:17
 */
public class ToolJob {
	private static final Log log = LogFactory.getLog(ToolJob.class);


    public static void toPdf(String imageFolderPath, String pdfPath) {
    	log.info("start");
        try {
            // 图片文件夹地址
            // String imageFolderPath = "D:/Demo/ceshi/";
            // 图片地址
            String imagePath = null;
            // PDF文件保存地址
            // String pdfPath = "D:/Demo/ceshi/hebing.pdf";
            // 输入流
            File pdf=new File(pdfPath);
            if(!pdf.exists()){
            	pdf.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(pdfPath);
            // 创建文档
            Document doc = new Document(null, 0, 0, 0, 0);
            //doc.open();
            // 写入PDF文档
            PdfWriter.getInstance(doc, fos);
            // 读取图片流
            BufferedImage img = null;
            // 实例化图片
            Image image = null;
            // 获取图片文件夹对象
            File file = new File(imageFolderPath);
                
            File[] files = file.listFiles();

            for (File file1 : files) {
                if (file1.getName().endsWith(".png")
                        || file1.getName().endsWith(".jpg")
                        || file1.getName().endsWith(".gif")
                        || file1.getName().endsWith(".jpeg")
                        || file1.getName().endsWith(".tif")) {
                    // System.out.println(file1.getName());
                    imagePath = imageFolderPath + file1.getName();
                    System.out.println(file1.getName());
                    // 读取图片流
                    img = ImageIO.read(new File(imagePath));
                    // 根据图片大小设置文档大小
                    doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
                    // 实例化图片
                    image = Image.getInstance(imagePath);
                    // 添加图片到文档
                    doc.open();
                    doc.add(image);
                }
            }
            // 关闭文档
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void toPdf(File file,String pdfPath){
    	try {
            // 图片地址
            String imagePath = file.toString();
           
            File pdf=new File(pdfPath);
            if(!pdf.exists()){
            	pdf.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(pdfPath);
            // 创建文档
            Document doc = new Document(null, 0, 0, 0, 0);
            //doc.open();
            // 写入PDF文档
            PdfWriter.getInstance(doc, fos);
            // 读取图片流
            BufferedImage img = null;
            // 实例化图片
            Image image = null;
            // 获取图片文件夹对象
         // 读取图片流
            img = ImageIO.read(new File(imagePath));
            // 根据图片大小设置文档大小
            doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
            // 实例化图片
            image = Image.getInstance(imagePath);
            // 添加图片到文档
            doc.open();
            doc.add(image);
            // 关闭文档
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
	/**
	 * 读取excel 字段是否重复、字段是否有空格
	 * @param sheet 表单好从0开始 
	 * @param column 列
	 */
	@SuppressWarnings("resource")
	public static void readExcel(String xlsPath,int startSheet,int endSheet,int column) {
//		List<String > all=new ArrayList<>();
		
		try {
			FileInputStream fileIn = new FileInputStream(xlsPath);
			Workbook wb=null;
			 if(ExcelUtil.checkVersion(xlsPath)){
                wb=new XSSFWorkbook(fileIn);
            }else{
                wb=new HSSFWorkbook(fileIn);
            }
		
			 String context,data = null;
			 int length=0,index = 0;
			 Map<String,Integer> key;
			 for(int i=startSheet;i<=endSheet;i++) {
				 Sheet sht0 = wb.getSheetAt(i); // 获取表单 从0开始
				 System.out.println("分栏名："+sht0.getSheetName());
//				StringBuffer buf=new StringBuffer();
				
				 key=new HashMap<>();
				 
				 
				for (Row r : sht0) {
					
					//过滤表单title
					if (r.getRowNum() < 2) {
						continue;
					}
					
					if(r.getCell(column)!=null) {
//							context=r.getCell(column).getStringCellValue();  //指定类型获取，类错误时，报错
						context=r.getCell(column).toString();  //统一字符串获取
						
						
						if(!key.containsKey(context)) {
							key.put(context, 0);
						}else {
							System.out.println(sht0.getSheetName()+" "+context+" Have Same");
						}
						
						if(context.length()>length) {
							data=context;
							length=data.length();
							index=r.getRowNum();
						}
						if(context.contains(" ")) {
							System.out.println(sht0.getSheetName()+" "+(r.getRowNum()+1)+" Have Null Space "+context);
						}
					}
					
				}
				System.out.println("分栏名："+sht0.getSheetName()+" 最长字符："+data+"长度："+length+" 行数："+(index+1));
			 }
			
			fileIn.close();
//			return buf.toString();
		} catch (FileNotFoundException e) {
			log.error("文件不存在或文件不可操作" + e,e);
		} catch (IOException e) {
			log.error("读写IO流异常" + e,e);
		} catch (Exception e) {
			log.error(e);
		}
	}
    
    
    public static void main(String[] args) {
    	String[] data= {"wer","2343"};
    	System.out.println(StringUtil.objToString(data));
    	System.out.println(StringUtil.objToString(Arrays.asList(data)));
    	
    	Map<String,Integer> dataMap=new HashMap<>();
    	dataMap.put("213", 1);
    	dataMap.put("223", 9);
    	System.out.println(StringUtil.objToString(dataMap));
    	System.out.println(StringUtil.objToString("2323"));
    	
    	Set<String> set = new HashSet<>();  
    	set.add("23");
    	set.add("us");
    	System.out.println(StringUtil.objToString(set));
    }

}
